package com.tydic.test;


import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;


public class FileImportDatabase {
	private String sConnStr = "";
	private String sDBDriver = "oracle.jdbc.OracleDriver";
	
	//数据库的用户名和密码
	private String userName = "pra";
	private String password = "pra";
	
	//文件路径
	private static String filePath = "./data/employee.txt";
	/**
	* 缺省构造器
	*/
	public FileImportDatabase() {
		sConnStr = "jdbc:oracle:thin:@HAWK:1521:orcl";
	}
	/**
	* @param ip,serviceName
	*/
	public FileImportDatabase(String ip,String serviceName) {
		sConnStr = "jdbc:oracle:thin:@"+ip+":1521:"+serviceName;
	}
	/**
	* 通过thin方式获得Oracle数据库的连接.
	*/
	public Connection connectDbByThin() {
		Connection conn=null;
		try {
			Class.forName(sDBDriver);
			conn = DriverManager.getConnection(sConnStr,userName,password);
		} catch (Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void main(String[] args) throws Exception {
		FileImportDatabase toi = new FileImportDatabase();
		Connection conn = toi.connectDbByThin();
	
		Statement stmt = null;
		stmt = conn.createStatement();
		
		Scanner in = new Scanner(new FileReader(filePath));
		
		String insertSql = "";
		String line = "";
		
		if (in != null) {
			while (in.hasNext()) {
				line = in.nextLine();
				String[] strs = line.split("\\|");
				
				if (strs != null && strs.length > 0) {
					insertSql = "insert into dest.t_filedata values('"+strs[0]+"','"+strs[1]+"','"+strs[2]+"')";
					
					stmt.executeUpdate(insertSql);
				}
			}
		}
	
		stmt.close();
		conn.close();
		
		System.out.println("OK");
		
	}

	
}
